<template>
  <div>
    <Modal v-model="show" :title="title" :mask-closable="false" :closable="false">
      <Form ref="modalForm" :model="data" :rules="ruls" :label-width="80">
        <FormItem label="姓名" prop="name">
          <Input disabled v-model.trim="user.name" type="text" autocomplete="false"></Input>
        </FormItem>
        <FormItem v-if="'my'==type" label="旧密码" prop="oldPassword">
          <Input v-model.trim="data.oldPassword" type="password" autocomplete="false"></Input>
        </FormItem>
        <FormItem label="新密码" prop="password">
          <Input v-model.trim="data.password" type="password" autocomplete="false"></Input>
        </FormItem>
        <FormItem label="重复密码" prop="rePassword">
          <Input v-model.trim="data.rePassword" type="password"></Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="default" :disabled="loading" @click="cancel(false)">取消</Button>
        <Button type="primary" :loading="loading" @click="ok">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      loading: false,
      data: {
        password: "",
        rePassword: ""
      },
      ruls: {
        password: [
          { required: true, message: "请填写密码" },
          {
            pattern: /^(\w){6,18}$/,
            message: "密码应为[A-Za-z0-9_]组成的6-18位字符"
          }
        ],
        rePassword: {
          validator: (rule, value, callback, source, options) => {
            if (value === "") {
              callback(new Error("请再次输入密码"));
            } else if (value !== this.data.password) {
              callback(new Error("两次输入密码不一致!"));
            } else {
              callback();
            }
          }
        },
        oldPassword: [{ required: true, message: "请填写旧密码" }]
      }
    };
  },
  props: {
    user: {
      type: Object,
      default: {}
    },
    type: {
      type: String,
      default: "sub"
    },
    title: {
      type: String,
      default: "重置密码"
    }
  },
  methods: {
    /**
     * @description 关闭Modal
     * @param reload 是否重新加载数据
     */
    cancel(reload = false) {
      this.$emit("cancel", "reset", reload);
    },
    ok() {
      this.$refs.modalForm.validate(valid => {
        if (valid) {
          this.resetPost({
            password: this.data.password,
            id: this.user.id,
            rePassword: this.data.rePassword,
            oldPassword:this.data.oldPassword
          });
        }
      });
    },
    resetPost(data) {
      var url = "/user/resetPassword";
      if(this.type=='my'){
        url = '/user/editPassword'
      }
      this.loading= true;
      var ps = Object.assign({}, data);

      this.post(url, ps, {}).then(res => {

        if (this.checkIsSsuccess(res)) {
          this.$Message.success({
            content: "my"!=this.type ?"重置成功！":"修改成功,请重新登陆",
            duration: 3,
            closable: true,
            onClose:()=>this.$emit("success")
          });
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 0,
            closable: true
          });
        }
        this.loading= false;
      });
    }
  }
};
</script>
